package com.sync.syncmasterdata.product.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sync.syncmasterdata.customer.pojo.OACustomer;
import com.sync.syncmasterdata.dingrobot.service.DingRobotService;
import com.sync.syncmasterdata.product.entity.BPM.InventoryLdtDeleteEntity;
import com.sync.syncmasterdata.product.entity.BPM.InventoryForItemSheetEntity;
import com.sync.syncmasterdata.product.entity.amu8.AMInvFreeContrapose;
import com.sync.syncmasterdata.product.entity.oa.ProductWorkflowDeleteEntity;
import com.sync.syncmasterdata.product.entity.oa.ProductWorkflowReStoreEntity;
import com.sync.syncmasterdata.product.entity.u8.InvEx;
import com.sync.syncmasterdata.product.entity.u8.InvFreeContrapose;
import com.sync.syncmasterdata.product.pojo.BPM.BPMForId;
import com.sync.syncmasterdata.product.pojo.bip.ProductForBip;
import com.sync.syncmasterdata.product.pojo.bip.ProductProdForBip;
import com.sync.syncmasterdata.product.pojo.bip.ProductStockForBip;
import com.sync.syncmasterdata.product.pojo.cp.InventoryForCP;
import com.sync.syncmasterdata.product.pojo.crm.InventoryForCrm;
import com.sync.syncmasterdata.product.pojo.erp.InventoryForU8;
import com.sync.syncmasterdata.product.pojo.gsp.InventoryForGsp;
import com.sync.syncmasterdata.product.pojo.oa.InventoryForOA;
import com.sync.syncmasterdata.product.service.BPMSynchronizationService;
import com.sync.syncmasterdata.product.service.ManualSyncInventoryService;
import com.sync.syncmasterdata.product.service.SyncInventoryService;
import com.sync.syncmasterdata.product.service.convert.*;
import com.sync.syncmasterdata.product.service.impl.BipProductServiceImpl;
import com.sync.syncmasterdata.util.DingMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 主数据--
 * 产品推送 控制层
 * productSync/oaSource
 */
@RestController
@RequestMapping("/productSync")
@Slf4j
public class ProductController {

    //钉钉服务
//    @Autowired
//    private DingRobotService dingRobotService;

    @Autowired
    private DingMessageUtil dingMessageUtil;

    @Autowired
    private ProductOAToBip productOAToBip;

    @Autowired
    private ProductOAToBipStock productOAToBipStock;

    @Autowired
    private ProductOAToBipProd productOAToBipProd;

    @Autowired
    private BipProductServiceImpl bipProductService;


    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private SyncInventoryService syncInventoryService;

    @Autowired
    private ProductOAToU8 productOaToU8Service;

    @Autowired
    private ProductOAtoCRM productOAtoCRMService;

    @Autowired
    private ProductOAtoWms productOAtoWms;

    @Autowired
    private ProductOAtoCP productOAtoCP;

    @Autowired
    private ProductOAToInvFree productOAToInvFree;

    @Autowired
    private ProductBPMToInvEx productBPMToInvEx;

    @Autowired
    private ProductBPMtoU8 productBPMtoU8Service;

    @Autowired
    private ProductOAToInvEx productOAToInvEx;

    @Autowired
    private ProductBPMtoCRM productBPMtoCRMService;

    @Autowired
    private ManualSyncInventoryService manualSyncInventoryService;

    @Autowired
    private BPMSynchronizationService bpmSynchronizationService;

    private static String[] SUSER = {"15267167276"};


    /**
     * OA 为 接受oa对象（用于手动接口调用）
     * {"requestid":"xxxx","prod","0"} 0=新增 1=修改 2=停用
     */
    @PostMapping("/oaSourceByRequestid")
    @ResponseBody
    public synchronized Map<String, Object> productSourceOaByRequestid(@RequestBody Map<String, Object> map) {
        try {
            StringBuilder sb = new StringBuilder();
            log.info("产品同步--接受下发信息:{}", objectMapper.writeValueAsString(map));
            if (!map.containsKey("requestid") || !map.containsKey("prod")) {
                return getReturn(-1, "请求报文出错");
            }
            //获取requestid
            String requestid = map.get("requestid").toString();
            //获取流程操作
            String prod = map.get("prod").toString();
            List<InventoryForOA> lists = null;

            if ("0".equals(prod)) {
                if (map.containsKey("huohao")) {
                    lists = syncInventoryService.getWorkflowInfo(requestid, map.get("huohao").toString());
                } else {
                    lists = syncInventoryService.getWorkflowInfo(requestid);
                }

                if (lists.isEmpty()) {
                    return getReturn(-1, "获取OA数据库流程记录异常");
                }
                log.info("需要推送的对象报文:{}", objectMapper.writeValueAsString(lists));
                for (InventoryForOA oa : lists) {
                    //获取 新产品 最后一个可用编码
                    String code = this.syncInventoryService.getLastCode(oa.getChanpinlx());
                    if ("-1".equals(code)) {
                        sb.append("产品<" + oa.getChanpinmc() + "> 无法获取产品分类，该产品未推送成功 \n ");
                        continue;
                    } else {
                        sb.append("产品<" + oa.getChanpinmc() + "> 获取编码为：" + code + "\n");
                    }
                    oa.setCunhuobm(code);
                    log.info("请求推送新增产品报文:{}", objectMapper.writeValueAsString(oa));

                    InventoryForU8 invU8 = productOaToU8Service.convertTo(oa);
                    //推送 博圣 U8
                    String u8id = syncInventoryService.addInvToOpenU8(invU8, "2");
                    //推送bip todo:计划12月 切换为先推送BIP
//                    handleBipMaterialFromOA(oa);

                    //推送 安迈 U8
                    syncInventoryService.addInvToOpenU8(invU8,"4");
                    //推送 美博特 U8
                    syncInventoryService.addInvToOpenU8(invU8,"6");

                    //增加170（博圣医学帐套的推送）JC类型的 U8升级后，医学产品同步 在转换方法中处理
//                    if (oa.getChanpinlx().contains("JC")) {
//                        InventoryForU8 invU8YX = new InventoryForU8();
//                        BeanUtils.copyProperties(invU8, invU8YX);
//                        //转换下单位
//                        invU8YX.setMain_measure(change170Unit(invU8YX.getMain_measure()));
//                        // *特殊需求 因为170帐套为 博圣医学专用帐套 没有自由项
//                        invU8YX.setSelf_define4("");
//                        invU8YX.setBconsiderfreestock(0);
//                        invU8YX.setBcontrolfreerange1(0);
//                        invU8YX.setFree_item1(0);
//                        //推送
//                        String u8idYX = syncInventoryService.addInvToOpenU8(invU8YX, "3");
//                        if (StringUtils.isBlank(u8idYX)) {
//                            sb.append("产品<" + oa.getChanpinmc() + "> 推送U8（医学170）产品信息异常 \n");
//                        }
//                    }

                    if (u8id == null) {
                        sb.append("产品<" + oa.getChanpinmc() + "> 推送U8产品信息异常 \n");
                        log.info("推送U8产品信息异常");
                        continue;
                    }
                    invU8.setCode(u8id);
                    oa.setCunhuobm(u8id);

                    //插入自由项(如果插入失败，手动处理)--增加 如果库位=检测项目库 不进行自由项的插入 同时禁止向wms新增
                    if ("检测项目库".equals(oa.getKuwei())) {
                        oa.setShifouts(null);
                    }
                    String _kuwei = oa.getKuwei();
                    //推送自由项(如果是 冷库，常温库，冷冻库推送，否则不推送)
                    if (("冷库".equals(_kuwei) || "常温库".equals(_kuwei) || "冷冻库".equals(_kuwei)) && !oa.getChanpinlx().contains("FW")) {
                        InvFreeContrapose invFreeContrapose = productOAToInvFree.convertTo(oa);
                        AMInvFreeContrapose aminvFreeContrapose = new AMInvFreeContrapose();
                        BeanUtils.copyProperties(invFreeContrapose, aminvFreeContrapose);
                        this.syncInventoryService.addInvFree(invFreeContrapose);
                        this.syncInventoryService.addAMU8InvFree(aminvFreeContrapose);
                    }

                    //插入自定义项(如果插入失败，手动处理)
                    InvEx invEx = productOAToInvEx.convertTo(oa);
                    if (oa.getChanpinlx().contains("JC")) {
                        //111 invex
                        this.syncInventoryService.addInvEx(invEx);
                        //170 invex
                        this.syncInventoryService.addInvExFor170(invEx);
                        //606 无自定义扩展项
                    } else {
                        this.syncInventoryService.addInvEx(invEx);
                    }

                    //oa 推送 crm
                    InventoryForCrm invCrm = productOAtoCRMService.convertTo(oa);
                    String crmid = "";
                    crmid = this.syncInventoryService.addInvToCrm(invCrm);
                    log.info("推送CRM 返回:{}", crmid);

                    //oa 推送 wms
                    String wmsid = "";
                    if (oa.getShifouts() != null) {
                        log.info("推送wms 对象：{}", objectMapper.writeValueAsString(oa));
                        //推送wms开关打开
                        //转换wms
                        //20200607增加多温度的转换
                        //20230714 从WMS 转为 GSP系统
                        InventoryForGsp[] invWms = productOAtoWms.convertTo(oa);
                        for (InventoryForGsp el : invWms) {
                            //之前的斐创
//                            String rspWms = syncInventoryService.addInvToWms(el);
                            String rspWms = syncInventoryService.addInvToGsp(el);
                            if ("0".equals(rspWms)) {
                                //重复异常
                                dingMessageUtil.pushMessage(null,"产品编码" + el.getGoodsCode() + "在GSP中重复(或异常),停止新增");
                            } else {
                                wmsid += el.getGoodsCode() + ",";
                            }
                        }
                    } else {
                        //代表不推送
                        wmsid = "0";
                    }
                    //oa 推送 cp
                    InventoryForCP cp = this.productOAtoCP.convertTo(oa);
                    log.info("ID展示:erp-{}||crm-{}||wms-{}", u8id, crmid, wmsid);
                    cp.setCrmid(StringUtils.isNotBlank(crmid) ? crmid : "");
                    cp.setErpid(StringUtils.isNotBlank(u8id) ? u8id : "");
                    cp.setWmsid(StringUtils.isNotBlank(wmsid) ? wmsid : "");
                    cp.setUserid("");
                    String oid = this.syncInventoryService.addInvToCP(cp);
                    sb.append("中台存货档案新增结果：" + oid + "\n");

                    //更新 oa产品入库流程 对应明细的存货编码
                    this.syncInventoryService.updateProductResult(oa.getId(), u8id);
                }
            }

            //产品修改
            if ("1".equals(prod)) {
                //oa流程 转换oa对象
                //获取转化列表
                lists = syncInventoryService.getWorkflowInfoForUpdate(requestid);
                if (lists.isEmpty()) {
                    return getReturn(-1, "(更新)获取OA数据库流程记录异常!!");
                }
                log.info("需要更新产品对象报文:{}", objectMapper.writeValueAsString(lists));
                for (InventoryForOA oa : lists) {

                    //ERP转换
                    InventoryForU8 invu8 = productOaToU8Service.convertToForUpdate(oa);
                    //博圣 U8更新
                    String ret = syncInventoryService.editInvToOpenU8(invu8, "2");
                    //安迈 U8 更新
                    syncInventoryService.editInvToOpenU8(invu8, "4");
                    //美博特 U8 更新
                    syncInventoryService.editInvToOpenU8(invu8,"6");
                    //更新自定义项
                    if (StringUtils.isNotBlank(ret)) {
                        InvEx _ex = productOAToInvEx.convertTo(oa);
                        syncInventoryService.updateInvEx(_ex);
                    } else {
                        sb.append("产品" + invu8.getCode() + ":ERP更新失败");
                        continue;
                    }
                    //CRM转换
                    InventoryForCrm crm = productOAtoCRMService.convertToForUpdate(oa);
                    //CRM更新
                    syncInventoryService.editInvToCrm(crm, oa.getCrmid());
                    //CP转换
                    InventoryForCP cp = this.productOAtoCP.convertToForUpdate(oa);
                    //CP更新
                    syncInventoryService.editInvToCP(cp);
                    //CP更新offer 产品信息
                    String _ret = syncInventoryService.editOfferInvToCP(cp);
                    if (_ret.contains("-1") || _ret.contains("-3") || _ret.contains("-2")) {
                        sb.append("更新Offer 价格档案结果-->" + _ret + "\n");
                    } else {
                        sb.append("更新Offer 价格成功");
                    }
                    sb.append("产品" + cp.getCunhuobm() + "更新成功\n");
                }
            }

            //产品停用
            if ("2".equals(prod)) {
                //获取oa 流程对象 开始转化
                log.info("开始执行停用");
                List<ProductWorkflowDeleteEntity> deleteEntityList = syncInventoryService.getWorkflowInfoforDelete(requestid);
                log.info("获取到oa 停用对象----->{}", objectMapper.writeValueAsString(deleteEntityList));
                if (deleteEntityList.isEmpty()) {
                    return getReturn(-1, "(停用)获取OA数据库流程记录异常!!!");
                }
                log.info("需要停用的产品对象报文------>{}", objectMapper.writeValueAsString(deleteEntityList));

                for (ProductWorkflowDeleteEntity entity : deleteEntityList) {
                    //ERP 停用
                    syncInventoryService.delInvToOpenU8(entity.getInventory_code(), "2");
                    //CRM 停用
                    String retCRM = syncInventoryService.delInvToCrm(entity.getCrmid());
                    //cp 产品档案管理器停用
                    String retBPM = syncInventoryService.delInvToCP(entity.getBpmid());
                    //cp 报价维护器停用
                    String retBPMOffer=syncInventoryService.delInvToCPOfferProduct(entity.getInventory_code());
                    log.info("停用 crm 产品(CRM产品档案)------>{}", retCRM);
                    log.info("停用 bpm 产品(产品档案维护)------>{}", retBPM);
                    log.info("停用 bpm 产品(报价维护器) ------>{}", retBPMOffer);
//                    sb.append("报价应用结果（" + retBPMOffer + "）");
                    sb.append("产品 停用成功!");
                }
            }

            //产品复用 only U8
            if ("3".equals(prod)) {
                //获取oa 流程对象 开始转化
                log.info("开始执行复用");
                List<ProductWorkflowReStoreEntity> reStoreEntityList = syncInventoryService.getWorkflowInfoforReStore(requestid);
                log.info("获取到oa 复用对象----->{}", objectMapper.writeValueAsString(reStoreEntityList));
                if (reStoreEntityList.isEmpty()) {
                    return getReturn(-1, "(复用 only U8)获取OA数据库流程记录异常!!!");
                }
                log.info("需要停用的产品对象报文------>{}", objectMapper.writeValueAsString(reStoreEntityList));

                for (ProductWorkflowReStoreEntity entity : reStoreEntityList) {
                    //ERP 停用
                    syncInventoryService.reStoreInvToOpenU8ByNextTime(entity, "2");
                    //CRM 停用
//                    String retCRM = syncInventoryService.delInvToCrm(entity.getCrmid());
                    //cp 产品档案管理器停用
//                    String retBPM = syncInventoryService.delInvToCP(entity.getBpmid());
//                    //cp 报价维护器停用
//                    String retBPMOffer=syncInventoryService.delInvToCPOfferProduct(entity.getInventory_code());
//                    log.info("停用 crm 产品(CRM产品档案)------>{}", retCRM);
//                    log.info("停用 bpm 产品(产品档案维护)------>{}", retBPM);
//                    log.info("停用 bpm 产品(报价维护器) ------>{}", retBPMOffer);
//                    sb.append("报价应用结果（" + retBPMOffer + "）");
                    sb.append("产品 复用(only U8)成功!");
                }
            }

            if ("4".equals(prod)) {
                //获取oa 流程对象 开始转化
                log.info("开始执行永久启用");
                List<ProductWorkflowReStoreEntity> reStoreEntityList = syncInventoryService.getWorkflowInfoforReStore(requestid);
                log.info("获取到oa 复用对象----->{}", objectMapper.writeValueAsString(reStoreEntityList));
                if (reStoreEntityList.isEmpty()) {
                    return getReturn(-1, "(复用 only U8)获取OA数据库流程记录异常!!!");
                }
                log.info("需要停用的产品对象报文------>{}", objectMapper.writeValueAsString(reStoreEntityList));

                for (ProductWorkflowReStoreEntity entity : reStoreEntityList) {
                    //ERP 复用
                    syncInventoryService.reStoreInvToOpenU8(entity, "2");
                    //CRM 复用
                    String retCRM = syncInventoryService.reStoreInvToCrm(entity.getCrmid());
//                    cp 产品档案管理器复用
                    String retBPM = syncInventoryService.reStoreInvToCP(entity.getBpmid());
//                    //cp 报价维护器复用
//                    String retBPMOffer=syncInventoryService.res(entity.getInventory_code());
                    log.info("永久启用 crm 产品(CRM产品档案)------>{}", retCRM);
                    log.info("永久启用 bpm 产品(产品档案维护)------>{}", retBPM);
//                    log.info("永久启用 bpm 产品(报价维护器) ------>{}", retBPMOffer);
//                    sb.append("报价应用结果（" + retBPMOffer + "）");
                    sb.append("产品 复用(only U8)成功!");
                }
            }
            dingMessageUtil.pushMessage(null,sb.toString());


        } catch (Exception e) {
            e.printStackTrace();
            log.info("停用异常---->{}", e.toString());
            return getReturn(-2, "throws Exception");
        }
        return getReturn(1, "产品档案推送完成");
    }

    //-------------以下为私有方法

    /**
     * 推送产品信息到bip中
     * @param inventoryForOA
     */
    public void handleBipMaterialFromOA(InventoryForOA inventoryForOA) {
        ProductForBip productToBip= productOAToBip.convertTo(inventoryForOA);
        String pk_material=bipProductService.addProduct(productToBip);
        String bipCode = bipProductService.getProductCode(pk_material);
        String[] argOrg = {"BS01", "BS0101", "BS0110", "BS0114", "BS0115"};
        bipProductService.assignMaterial(bipCode, argOrg);
        ProductStockForBip productStockForBip = productOAToBipStock.convertTo(inventoryForOA);
        ProductProdForBip productProdForBip = productOAToBipProd.convertTo(inventoryForOA);
        boolean flag1 = bipProductService.stockAdd(argOrg, productStockForBip);
        boolean flag2 = bipProductService.prodAdd(argOrg, productProdForBip);
        dingMessageUtil.pushMessage(null, "stockadd:" + flag1 + "|prodAdd:" + flag2);

    }

    /**
     * 临时 使用接口 手动处理 产品 同步
     *
     * @param productCode
     */
    @PostMapping("/handlerManualProduct")
    public String handlerManualProductSync(@RequestParam String productCode) {
        if (StringUtils.isNotBlank(productCode)) {
            return "参数错误!!!";
        }
        String ret = manualSyncInventoryService.handlerProductErpHas(productCode);
        return ret;
    }

    /**
     * BPM产品同步接口
     * 新增 变更 停用
     *
     * @param entity
     */
    @Deprecated
    @PostMapping("/BPMproduct_push")
    public String productPush(@RequestBody BPMForId entity) {
        log.info("接收到的信息：{}", entity.toString());
        Boolean exist = typeConversion(entity.getIf_jc_exist());
        try {
            StringBuilder sb = new StringBuilder();
            //产品新增执行
            if (entity.getUpdate_type().contains("新增") && !exist) {
                //获取新增产品信息
                List<InventoryForItemSheetEntity> productinformation = bpmSynchronizationService.getProductInformation(entity.getId());
                //循环执行新增产品信息
                for (InventoryForItemSheetEntity bpm : productinformation) {
                    //获取 新产品 最后一个可用编码
                    String code = this.syncInventoryService.getLastCode(bpm.getProduct_type_id());
                    if ("-1".equals(code)) {
                        sb.append("产品<" + bpm.getProduct_name() + "> 无法获取产品分类，该产品未推送成功 \n ");
                        continue;
                    } else {
                        sb.append("产品<" + bpm.getProduct_name() + "> 获取编码为：" + code + "\n");
                    }
                    //存货编码写入产品信息内
                    bpm.setInventory_code_txt(code);
                    log.info("请求推送新增产品报文:{}", objectMapper.writeValueAsString(bpm));
                    //bpm产品信息转换为U8格式
                    InventoryForU8 invU8 = productBPMtoU8Service.convertTo(bpm);
                    //获取执行结果
                    String u8id = syncInventoryService.addInvToOpenU8(invU8, "2");
                    //u8id为 null则推送异常
                    if (u8id == null) {
                        sb.append("产品<" + bpm.getProduct_name() + "> 推送U8产品信息异常 \n");
                        log.info("推送U8产品信息异常");
                        continue;
                    }
                    invU8.setCode(u8id);
                    //u8id写入产品信息内
                    bpm.setInventory_code_txt(u8id);

                    //插入自定义项(如果插入失败，手动处理)
                    InvEx invEx = productBPMToInvEx.convertTo(bpm);
                    if (bpm.getBusiness_type().contains("LDT")) {
                        //111 invex
                        this.syncInventoryService.addInvEx(invEx);
                        //170 invex
                        this.syncInventoryService.addInvExFor170(invEx);
                    } else {
                        this.syncInventoryService.addInvEx(invEx);
                    }

                    //oa 推送 crm
                    InventoryForCrm invCrm = productBPMtoCRMService.convertTo(bpm);
                    String crmid = "";
                    //执行crm产品新增
                    crmid = this.syncInventoryService.addInvToCrm(invCrm);
                    log.info("推送CRM 返回:{}", crmid);
                    //crmid写入产品信息内
                    bpm.setCRM_id(crmid);
                    //根据明细表id更新erpid，crmid
                    bpmSynchronizationService.getUpdateLdtItemSheet(bpm.getCRM_id(), bpm.getInventory_code_txt(), bpm.getId());

                }
            }

            //产品修改
            if (entity.getUpdate_type().contains("变更")) {
                //获取需要修改的产品信息
                List<InventoryForItemSheetEntity> productinformation = bpmSynchronizationService.getProductInformation(entity.getId());
                //循环执行修改
                for (InventoryForItemSheetEntity bpm : productinformation) {

                    //ERP转换
                    InventoryForU8 invu8 = productBPMtoU8Service.convertTo(bpm);
                    //ERP更新
                    String ret = syncInventoryService.editInvToOpenU8(invu8, "2");
                    //更新自定义项
                    if (StringUtils.isNotBlank(ret)) {
                        InvEx _ex = productBPMToInvEx.convertTo(bpm);
                        syncInventoryService.updateInvEx(_ex);
                    } else {
                        sb.append("产品" + invu8.getCode() + ":ERP更新失败");
                        continue;
                    }
                    //CRM转换
                    InventoryForCrm crm = productBPMtoCRMService.convertTo(bpm);
                    //CRM更新
                    syncInventoryService.editInvToCrm(crm, bpm.getCRM_id());
                }
            }

            //产品停用
            if (entity.getUpdate_type().contains("停用")) {
                //获取oa 流程对象 开始转化
                log.info("开始执行停用");
                //获取到需要删除的产品信息
                List<InventoryLdtDeleteEntity> LdtDeleteEntities = bpmSynchronizationService.getDelectProduct(entity.getId());
                //判断获取的信息是否为空
                if (LdtDeleteEntities.isEmpty()) {
                    log.info("(停用)获取BPM数据库流程记录异常!!!");
                }
                log.info("需要停用的产品对象报文------>{}", LdtDeleteEntities.toString());
                //循环执行停用接口
                for (InventoryLdtDeleteEntity lists : LdtDeleteEntities) {
                    //ERP 停用
                    syncInventoryService.delInvToOpenU8(lists.getInventory_code_txt(), "2");
                    //CRM 停用
                    String retCRM = syncInventoryService.delInvToCrm(lists.getCrm_id());
                    log.info("停用 crm 产品 retCRM------>{}", retCRM);
                    sb.append("产品 停用成功!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("停用异常---->{}", e.toString());
            return null;
        }
        bpmSynchronizationService.getUpdatePushed(entity.getId());
        return null;
    }


    /**
     * OA 为 接受oa对象（用于手动接口调用）暂时不用
     * 测试用
     */
    @Deprecated
    @ResponseBody
    private Map<String, Object> productSourceOa(@Validated @RequestBody InventoryForOA oa) {
        try {
            log.info("产品同步--接受下发信息:{}", objectMapper.writeValueAsString(oa));
            //获取该分类下的可用的产品编码

            //oa 推送 u8 (1-转换。2-推送。3-更新id)
            InventoryForU8 invU8 = productOaToU8Service.convertTo(oa);
            String u8id = syncInventoryService.addInvToOpenU8(invU8, "1");
            if (u8id == null) {
                log.info("推送U8产品信息异常");
                return getReturn(-1, "推送U8产品信息异常");
            }
            invU8.setCode(u8id);
            oa.setCunhuobm(u8id);
            //插入自由项(如果插入失败，手动处理)
            InvFreeContrapose invFreeContrapose = productOAToInvFree.convertTo(oa);
            this.syncInventoryService.addInvFree(invFreeContrapose);

            //oa 推送 crm
            InventoryForCrm invCrm = productOAtoCRMService.convertTo(oa);
            String crmid = this.syncInventoryService.addInvToCrm(invCrm);
            log.info("推送CRM 返回:{}", crmid);

            //oa 推送 wms

            //oa 推送 cp


        } catch (Exception e) {
            e.printStackTrace();
            return getReturn(-2, "throws Exception");
        }
        return getReturn(1, "产品档案推送完成");
    }


    /**
     * 606(或测试账套推送用)账套 使用新增推送,手动用
     * 作废
     *
     * @param map
     * @return
     */
    @Deprecated
    public Map<String, Object> productPushToTestAPI(@RequestBody Map<String, Object> map) {
        StringBuilder sb = new StringBuilder();
        String requestid = null;
        String prod = null;
        List<InventoryForOA> lists = null;
        try {
            log.info("产品同步--接受下发信息:{}", objectMapper.writeValueAsString(map));
            if (!map.containsKey("requestid") || !map.containsKey("prod")) {
                return getReturn(-1, "请求报文出错");
            }
            //获取requestid
            requestid = map.get("requestid").toString();
            //获取流程操作
            prod = map.get("prod").toString();
            lists = null;
            if ("0".equals(prod)) {
                if (map.containsKey("huohao")) {
                    lists = syncInventoryService.getWorkflowInfo(requestid, map.get("huohao").toString());
                } else {
                    lists = syncInventoryService.getWorkflowInfo(requestid);
                }

                if (lists.isEmpty()) {
                    return getReturn(-1, "获取OA数据库流程记录异常");
                }
                log.info("需要推送的对象报文:{}", objectMapper.writeValueAsString(lists));
                for (InventoryForOA oa : lists) {
                    InventoryForU8 invU8 = productOaToU8Service.convertTo(oa);
                    //增加606杰毅账套 的SJ,HC,YQ分类的推送
                    if (oa.getChanpinlx().contains("SJ") || oa.getChanpinlx().contains("HC01") || oa.getChanpinlx().contains("YQ")) {
                        InventoryForU8 invU8JY = new InventoryForU8();
                        BeanUtils.copyProperties(invU8, invU8JY);
                        //分类调整
                        invU8JY.setSort_code(invU8JY.getSort_code() + "00");
                        //备件
                        invU8JY.setBequipment(1);
                        //自制
                        invU8JY.setSelfmake_flag(1);
                        //委托
                        invU8JY.setBProxyForeign(1);
                        //注册证
                        invU8JY.setSelf_define1(oa.getRegistrationNo());
                        //货号
                        invU8JY.setSelf_define2(oa.getHuohao());
                        //生产厂家
                        invU8JY.setSelf_define3(oa.getShengchanqywb());
                        //说明
                        invU8JY.setSelf_define4(oa.getExplain());
                        //技术要求
                        invU8JY.setSelf_define5(oa.getTechnicalRequire());
                        //温度要求
                        invU8JY.setSelf_define6(oa.getTemperatureRequire());
                        //取消扩展项
                        invU8JY.setCidefine2(null);
                        invU8JY.setCidefine3(null);
                        invU8JY.setCidefine4(null);
                        invU8JY.setCidefine5(null);
                        String u8idJY = syncInventoryService.addInvToOpenU8(invU8JY, "4");
                        if (StringUtils.isBlank(u8idJY)) {
                            sb.append("产品<" + oa.getChanpinmc() + "> 推送U8（杰毅606）产品信息异常 \n");
                        }

                    }
                    System.out.println("账套存货新增结果----->" + sb.toString());
                }
            }

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return getReturn(1, "执行完成");
    }

    /**
     * 是否在同一JC码上新增所对应的检测项目号转换
     *
     * @param if_jc_exist
     * @return
     */
    private boolean typeConversion(Object if_jc_exist) {
        try {
            String tmp = String.valueOf(if_jc_exist);

            if (tmp.equals("否")) {
                return false;
            }
            if (tmp.equals("是")) {
                return true;
            }
            if (tmp.equals("0")) {
                return false;
            }
            if (tmp.equals("1")) {
                return true;
            }
            if (tmp.equals("true")) {
                return false;
            }
            if (tmp.equals("false")) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
        return true;
    }

    /**
     * 返回值 整理
     *
     * @param errcode
     * @param errmsg
     * @return
     */
    private Map<String, Object> getReturn(int errcode, String errmsg) {
        Map<String, Object> map = new HashMap<>();
        map.put("errcode", errcode);
        map.put("errmsg", errmsg);
        return map;
    }

    /**
     * 170 特殊单位的转换 大盒->人份的转换
     */
    private String change170Unit(String s) {
        if (s.equals("038")) {
            return "023";
        }
        if (s.equals("023")) {
            return "038";
        }
        return s;
    }
}
